#!/bin/bash

set -x

if [ $# -eq 0 ]; then
    echo "Usage: $0 [api|stream|all]"
    echo "  api   - Build API server only"
    echo "  stream - Build stream server only"
    echo "  all   - Build both servers"
    exit 1
fi

BUILD_TARGET=$1

build_api() {
    echo "Compiling API server..."
    go build -o api_server.exe ./api
    
    if [ $? -eq 0 ]; then
        echo "API server build successful! Executable created as api_server.exe"
    else
        echo "API server build failed"
        exit 1
    fi
}

build_stream() {
    echo "Compiling stream server..."
    go build -o stream_server.exe ./streamserver
    
    if [ $? -eq 0 ]; then
        echo "Stream server build successful! Executable created as stream_server.exe"
    else
        echo "Stream server build failed"
        exit 1
    fi
}

case $BUILD_TARGET in
    api)
        build_api
        ;;
    stream)
        build_stream
        ;;
    all)
        build_api
        build_stream
        ;;
    *)
        echo "Invalid target: $BUILD_TARGET"
        echo "Usage: $0 [api|stream|all]"
        exit 1
        ;;
esac